﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
//using System.Xml.Linq;
using System.Collections.Generic;

using YouHP.BLL;
using YouHP.Model;
using YouHP.Utility;

namespace web.admin
{
    public partial class UserActivity : System.Web.UI.Page
    {
        private UserActivityBLL userActivityBll = UserActivityBLL.Instance;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.ViewState["keyWordParm"] = "";
                this.ViewState["activityId"] = RequestTools.GetInt("id", -1) + "";

                bind(this.ViewState["keyWordParm"].ToString());
            }
        }

        protected void search_Click(object sender, EventArgs e)
        {
            this.ViewState["keyWordParm"] = this.keyWord.Text.Trim();

            bind(this.ViewState["keyWordParm"].ToString());
        }

        private void bind(string keyWord)
        {
            List<UserActivityInfo> list = null;

            try
            {
                int activityId = Convert.ToInt32(this.ViewState["activityId"]);
                if (activityId > 0)
                {
                    ActivityNewsInfo info = ActivityNewsBLL.Instance.GetActivityNewsInfo(activityId);
                    if (info != null)
                    {
                        this.activityTitle.Text = info.Title;
                        list = userActivityBll.GetAllUserActivityByActivityId(activityId, keyWord);
                        for (int i = 0; i < list.Count; i++)
                        {
                            if (list[i].IsVerify == Constants.NEEDCHECK_ACTIVITY)
                            {
                                list[i].Status = "需要审批";
                                list[i].Operation = "审批";
                                list[i].OptLinkUrl = "UserActivityCheck.aspx?id=" + list[i].Id ;
                            }
                            else if(list[i].IsVerify == Constants.PASS_ACTIVITY)
                            {
                                list[i].Status = "已审批通过";
                                list[i].Operation = "查看";
                                list[i].OptLinkUrl = "UserActivityView.aspx?id=" + list[i].Id;
                            }
                            else if (list[i].IsVerify == Constants.FAIL_ACTIVITY)
                            {
                                list[i].Status = "未审批通过";
                                list[i].Operation = "查看";
                                list[i].OptLinkUrl = "UserActivityView.aspx?id=" + list[i].Id;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            this.userActivityList.DataSource = list;
            this.userActivityList.DataBind();
        }

        protected void DeleteBtn_Click(object sender, EventArgs e)
        {
            try
            {
                int[] checkList = new int[this.userActivityList.Rows.Count];
                int count = 0;
                for (int rowIndex = 0; rowIndex < this.userActivityList.Rows.Count; rowIndex++)
                {
                    bool isCheck = ((CheckBox)this.userActivityList.Rows[rowIndex].Cells[0].FindControl("chk")).Checked;
                    if (isCheck)
                    {
                        count++;
                        checkList[rowIndex] = Convert.ToInt32(this.userActivityList.DataKeys[rowIndex].Value);
                    }
                }

                int[] idList = new int[count];
                for (int i = 0, j = 0; i < checkList.Length; i++)
                {
                    if (checkList[i] > 0)
                        idList[j++] = checkList[i];
                }

                userActivityBll.DeleteUserActivity(idList);
            }
            catch (Exception ex)
            {
                throw ex;
            }

            bind(this.ViewState["keyWordParm"].ToString());
        }

        protected void pageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.userActivityList.PageIndex = e.NewPageIndex;
            bind(this.ViewState["keyWordParm"].ToString());
        }
    }
}

